package com.camel.base.utils;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.camel.base.model.ZsInformation;
import com.camel.core.exceptions.ExcelException;
import com.camel.core.utils.ExcelUtil;
import lombok.Data;
import org.apache.ibatis.util.MapUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;

@Data
public class ImportUtil {

    public static Map<String, String> transWordsMap = new HashMap<>();
    
    public ImportUtil initTransMap(Map<String, String> map) {
        if(ObjectUtils.isEmpty(map)) {
            ImportUtil.transWordsMap = map;
        }
        return this;
    }
    
    private MultipartFile file;

    private Integer surveyId;

    public ImportUtil(MultipartFile file, Integer id) {
        this.file = file;
        this.surveyId = id;
    }

    public List<ZsInformation> readInfomations() {
        // 存储excel数据
        List<ZsInformation> list = new ArrayList<>();
        Workbook wookbook = null;
        InputStream inputStream = null;

        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new ExcelException("文件读取异常");
        }

        // 根据excel文件版本获取工作簿
        if (file.getOriginalFilename().endsWith(".xls")) {
            wookbook = ExcelUtil.xls(inputStream);
        } else if (file.getOriginalFilename().endsWith(".xlsx")) {
            wookbook = ExcelUtil.xlsx(inputStream);
        } else {
            throw new ExcelException("非excel文件");
        }

        // 得到一个工作表
        Sheet sheet = wookbook.getSheetAt(0);
        // 获取行总数
        int rows = sheet.getLastRowNum() + 1;
        // 获取首行
        Row head = sheet.getRow(0);
        JSONObject json = new JSONObject();

        Row row;
        for (int i = 1; i < rows; i++) {
            ZsInformation zsInformation = new ZsInformation();
            zsInformation.setSurveyId(surveyId);
            row = sheet.getRow(i);
            row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
            zsInformation.setPhone(row.getCell(0).getStringCellValue());
            Set<String> strings = json.keySet();
            for (int j = 1; j < head.getLastCellNum(); j++) {
                head.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                json.put(head.getCell(j).getStringCellValue(), row.getCell(j).getStringCellValue());
            }
//            for (String string : strings) {
//                if(transWordsMap.containsKey(string)) {
//                    String sValue = json.getString(string);
//                    json.remove(string);
//                    String s = transWordsMap.get(string);
//                    json.put(s, sValue);
//                }
//            }
            zsInformation.setInfomation(json.toJSONString());
            zsInformation.setCreateAt(new Date());
            zsInformation.setCreator(StpUtil.getLoginIdAsInt());
            zsInformation.setUpdateAt(new Date());
            zsInformation.setUpdator(StpUtil.getLoginIdAsInt());
            list.add(zsInformation);
        }

        return list;
    }
}
